package com.dhl.fin.api.controller;

import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.domain.BankAccount;
import com.dhl.fin.api.dto.BankCardDataResponseDTO;
import com.dhl.fin.api.dto.PaySuccessRequestDTO;
import com.dhl.fin.api.enums.PayStatusEnum;
import com.dhl.fin.api.service.BankAccountService;
import com.dhl.fin.api.service.PaymentRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@Slf4j
@RequestMapping("bankaccount")
public class BankAccountController extends CommonController<BankAccount> {

    @Autowired
    private BankAccountService bankAccountService;

    @Autowired
    private PaymentRecordService paymentRecordService;

    /**
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("paysuccess")
    public ApiResponse addMoreExpireDate(@RequestBody PaySuccessRequestDTO paySuccessRequestDTO) throws Exception {

        log.info("paySuccessRequestDTO:{}", paySuccessRequestDTO);

        //延长银行卡到期时间
        bankAccountService.extendExpireDate(paySuccessRequestDTO);

        //更新支付单状态
        paymentRecordService.updatePayStatus(paySuccessRequestDTO.getPayStatus(), paySuccessRequestDTO.getOutTradeNo());

        return ApiResponse.success();
    }

    /**
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("payerror")
    public ApiResponse addMoreExpireDate(String outTradeNo) throws Exception {

        log.info("支付失败outTradeNo:{}", outTradeNo);

        //更新支付单状态
        paymentRecordService.updatePayStatus(PayStatusEnum.ERROR.getCode(), outTradeNo);

        return ApiResponse.success();
    }


    @ResponseBody
    @GetMapping("bankcarddata")
    public ApiResponse getHomeBankInfo(String unicode) throws Exception {
        List<BankCardDataResponseDTO> bankCardDataResponseDTOS = bankAccountService.getBankCardData(unicode);
        return ApiResponse.success(bankCardDataResponseDTOS);
    }


}
